package com.kantboot.system.user.service.impl;

import com.kantboot.system.user.domain.entity.SysUser;
import com.kantboot.system.user.dao.repository.entity.SysUserRepository;
import com.kantboot.system.user.service.ISysUserRoleService;
import com.kantboot.system.user.service.ISysUserService;
import com.kantboot.system.user.setting.SystemUserSetting;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class SysUserServiceImpl implements ISysUserService {

    @Resource
    private SysUserRepository repository;

    @Resource
    private ISysUserRoleService userRoleService;

    @Resource
    private SystemUserSetting setting;


    @Override
    public SysUser createNewUser(SysUser user) {
        if(user==null){
            user = new SysUser();
        }
        user.setId(null);
        SysUser save = repository.save(user);
        userRoleService.addUserRole(save.getId(),setting.getDefaultRoleCode());
        return repository.findById(save.getId()).orElseThrow();
    }

    @Override
    public Optional<SysUser> findUserId(Long selfId) {
        return repository.findById(selfId);
    }

}
